package com.cloudcc.mobile.weight;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.util.AttributeSet;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@SuppressLint({ "NewApi", "SetJavaScriptEnabled" })
public class MyWebView extends WebView {

	private OnLoadCallBack onLoadCallBack;
	private MyWebViewClient client;

	public static class OnLoadCallBack {
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
		};

		public void onPageFinished(WebView view, String url) {
		};

		public void onReceivedError() {
		};

		public void onProgressChanged(int newProgress) {
		};
	}

	public void setOnLoadListener(OnLoadCallBack onLoadCallBack) {
		this.onLoadCallBack = onLoadCallBack;
	}

	@SuppressWarnings("deprecation")
	public MyWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
		try {
			setLayerType(LAYER_TYPE_SOFTWARE, null);
		} catch (NoSuchMethodError e) {
		}
		cancelLongPress();
		setVerticalScrollBarEnabled(false);
		setHorizontalScrollBarEnabled(false);
		WebSettings settings = getSettings();
		if (settings != null) {
			settings.setRenderPriority(RenderPriority.HIGH);
			settings.setJavaScriptEnabled(true);
		}
		client = new MyWebViewClient();
		setWebViewClient(client);
		setWebChromeClient(new MyWebChromeClient());
	}

	@Override
	public void loadUrl(String url) {
		client.start();
		super.loadUrl(url);
	}

	private class MyWebChromeClient extends WebChromeClient {
		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			if (onLoadCallBack != null) {
				onLoadCallBack.onProgressChanged(newProgress);
			}
		}
	}

	private class MyWebViewClient extends WebViewClient {
		private boolean isError = false;
		private boolean isLoad = false;

		public void start() {
			isError = false;
			isLoad = true;
		}

		@Override
		public void onReceivedSslError(WebView view, SslErrorHandler handler,
				SslError error) {
			handler.proceed();
		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			if (url != null && url.indexOf("javascript") != 0) {
				view.loadUrl(url);
				return true;
			} else {
				return false;
			}
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			if (onLoadCallBack != null) {
				if (isLoad) {
					if (isError) {
						onLoadCallBack.onReceivedError();
					} else {
						onLoadCallBack.onPageFinished(view, url);
					}
					isLoad = false;
				}
			}
		}

		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			isError = true;
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			if (onLoadCallBack != null && !isError) {
				onLoadCallBack.onPageStarted(view, url, favicon);
			}
		}
	}
}